package edu.cqut.customlistviewdemo;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class MainActivity extends AppCompatActivity {
    static List<Map<String, Object>> mfoodinfo; //菜品数据源列表，由HashMap表构成
    public ListView mlistview;
    static SimpleAdapter mlistItemAdapter;
    public ArrayList<Dish> mDishes = new ArrayList<Dish>();  //菜品列表

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //生成菜单信息列表
        FillDishesList();

        mlistview = (ListView) findViewById(R.id.ListViewDemo);
        mfoodinfo=getFoodData();
        //构造SimpleAdapter适配器，将它和ListView自定义的布局文件、List数据源关联
        mlistItemAdapter = new SimpleAdapter(this,
                mfoodinfo,//数据源 ，列表的每一节对应ListView的每一行
                R.layout.listitem,//ListItem的XML实现
                //动态数组与listitem对应的子项必须与  mfoodinfo中的各资源名字一致
                new String[] {"image", "title", "info"},
                //listitem的XML文件里面的1个ImageView,3个TextView ID
                new int[]{ R.id.img, R.id.title, R.id.info});

        mlistItemAdapter.notifyDataSetChanged();
        mlistview.setAdapter(mlistItemAdapter);

        //设置ListView选项点击监听器
        this.mlistview.setOnItemClickListener(new OnItemClickListener(){
            @Override
            public void onItemClick(AdapterView<?> arg0, //选项所属的ListView
                                    View arg1, //被选中的控件，即ListView中被选中的子项
                                    int arg2, //被选中子项在ListView中的位置
                                    long arg3) //被选中子项的行号
            {
                ListView templist = (ListView)arg0;
                View mView = templist.getChildAt(arg2);//选中子项(即item)在listview中的位置
                final TextView tvTitle = (TextView)mView.findViewById(R.id.title);
                Toast.makeText(MainActivity.this, tvTitle.getText().toString(), Toast.LENGTH_LONG).show();
            }
        });
    }

    private void FillDishesList()
    {
        Dish theDish = new Dish();
        //添加菜品
        theDish.mName = "宫保鸡丁";
        theDish.mInfo = "北京宫廷菜，入口鲜辣香脆";
        theDish.mImage = (R.raw.food01gongbaojiding);
        mDishes.add(theDish);

        theDish = new Dish();
        theDish.mName = "椒盐玉米";
        theDish.mInfo = "色香味俱全，浙江菜";
        theDish.mImage = (R.raw.food02jiaoyanyumi);
        mDishes.add(theDish);

        theDish = new Dish();
        theDish.mName = "清蒸武昌鱼";
        theDish.mInfo = "湖北鄂州传统名菜";
        theDish.mImage = (R.raw.food03qingzhengwuchangyu);
        mDishes.add(theDish);

        theDish = new Dish();
        theDish.mName = "鱼香肉丝";
        theDish.mInfo = "经典汉族传统川菜";
        theDish.mImage = (R.raw.food04yuxiangrousi);
        mDishes.add(theDish);
    }

    private ArrayList<Map<String, Object>> getFoodData()
    {
        ArrayList<Map<String, Object>> fooddata=new ArrayList<Map<String,Object>>();
        //将菜品信息填充进foodinfo列表
        int s = mDishes.size(); //得到菜品数量
        for (int i=0; i<s; i++) {
            Dish theDish = mDishes.get(i); //得到当前菜品
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("image", theDish.mImage);
            map.put("title", theDish.mName);
            map.put("info", theDish.mInfo);
            fooddata.add(map);
        }
        return fooddata;
    }
}